$(function(){
    var orderBillList_datagrid;
    orderBillList_datagrid = $("#orderBillList_datagrid");

    orderBillList_datagrid.datagrid({
        url:'/orderBill/list?state=0',

        fitColumns:true,
        toolbar:'#datagrid_toolar',
        pagination:true,
        rownumbers:true,
        singleSelect:true,
        columns:[[
            {field:'orderBillNumber',title:'申请单号',width:60},
            {field:'client',title:'投保人姓名',width:100,formatter:applyClientFormatter},
            {field:'carNumber',title:'车牌号码',width:100},
            {field:'product',title:'产品',width:100,formatter:productFormatter},
            {field:'applyTime',title:'投保申请时间',width:100}
        ]]
    });
    /*organizationId:organizationId,
     productId:productId,*/
    var cmdObj={
        //搜索
        orderBillList_search:function(){
            var organizationId=$("[name=organizationId]").val()
            var productId=$("[name=productId]").val()
            var idCardNumber=$("[name=idCardNumber]").val()
            var orderBillNumber=$("[name=orderBillNumber]").val()
            var realName=$("[name=realName]").val()
            var carNumber=$("[name=carNumber]").val()

            var beginTime_fullYear = new Date($("#beginTime").datebox("getValue")).getFullYear();
            var beginTime_month = new Date($("#beginTime").datebox("getValue")).getMonth();
            var beginTime_day = new Date($("#beginTime").datebox("getValue")).getDate();
            var beginTime_date = beginTime_fullYear +"/"+ (beginTime_month+1)+"/"+beginTime_day;

            var endTime_fullYear = new Date($("#endTime").datebox("getValue")).getFullYear();
            var endTime_month = new Date($("#endTime").datebox("getValue")).getMonth();
            var endTime_day = new Date($("#endTime").datebox("getValue")).getDate();
            var endTime_date = endTime_fullYear +"/"+ (endTime_month+1)+"/"+endTime_day;

            //var beginTime=$("[name=beginTime]").val()
            //var endTime=$("[name=endTime]").val()
             if(beginTime_date!=NaN||endTime_date!=NaN||!organizationId||!productId) {
                $.messager.alert("温馨提示","请选择必填选项","info")
            }else {
                orderBillList_datagrid.datagrid("load",{
                    organizationId:organizationId,
                    productId:productId,
                    idCardNumber:idCardNumber,
                    orderBillNumber:orderBillNumber,
                    realName:realName,
                    beginTime:beginTime_date,
                    endTime:endTime_date,
                    carNumber:carNumber

                })
            }

        },
        //修改
        orderBillList_edit:function(){
            var selectedRow = orderBillList_datagrid.datagrid("getSelected");
            if (selectedRow){
                console.log(selectedRow.id);
                window.location.href='/orderBill/orderBillEdit?orgId='+selectedRow.id
            }else {
                $.messager.alert("温馨提示","请选择要修改的数据","info")
            }
        },
        //删除
        carInfo_quit:function(){

        },
        //重新加载
        orderBillList_reload:function(){
            window.location.reload()
        },
        //暂存单页面的申请批改按钮
        orderBillList_checkApply:function(){
            var selectedRow = orderBillList_datagrid.datagrid("getSelected");
            console.log(selectedRow)
            if (selectedRow){
                $("#dialog_checkApply").dialog("open")
                //window.location.href='/orderBill/orderBillEdit?orgId='+selectedRow.id
            }else {
                $.messager.alert("温馨提示","请选择要修改的数据","info")
            }
        }
    };
    $("#list_organization_id").combobox({
        onChange:function(){
            var id = $("#list_organization_id").combobox("getValue");

            $("#list_product_id").combobox({
                onBeforeLoad: function(param){
                    param.orgId = id;
                }
            })
            /*$("#product_id").combobox("reload")*/
        }
    })
    $("a[data-cmd]").on("click",function(){
        var cmd = $(this).data("cmd");
        cmdObj[cmd]();
    })
    $("#dialog_checkApply").dialog({
        title: '批改申请说明',
        width: 330,
        height: 230,
        buttons:'#dialog_checkApplybtn',
        closed: true
    })
});
//*******************************************************
function applyClientFormatter(value,row,index){
    return value==null?'':value.realName;
}
function productFormatter(value,row,index){
    return value==null?'':value.name;
}
//申请批改按钮
function check_apply(){
    $("#dialog_checkApply").dialog("open")

}

//信息填写完成确认
function checkApply_ok(){
    var text = $("#applyText").textbox("getText");
    var selectedRow = $("#orderBillList_datagrid").datagrid("getSelected");
    $.messager.confirm("温馨提示","是否确认提交批改",function(ok){
        if(ok){
            $.post("/orderBill/checkApply",{orderId:selectedRow.id,remark:text},function(data){
                if (data.success){
                    window.location.reload()
                    $("#dialog_checkApply").dialog("close")
                    $.messager.alert("温馨提示",data.msg,"info")
                }else {
                    $.messager.alert("温馨提示",data.msg,"error")
                }
            },"json")
        }
    })

}
//信息填写完成确认
function checkApply_cancel(){
    $("#dialog_checkApply").dialog("close")
}